﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PDAMain.DAL;
using PDAMain.Entity;
using System.Runtime.InteropServices;
using PDAMain.Interface;

namespace PDAMain.Forms.ReadingWatermeter
{
    public partial class WaterPlanList : BaseForm
    {
        public WaterPlanList()
        {
            InitializeComponent();
        }

        private void WaterPlanList_Load(object sender, EventArgs e)
        {
            BindData();
        }
        private void BindData() 
        {
            DataSet ds = DataHelper.ReadingWatermeterDao.listPlan();
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {


                DataTable dtmapping = ds.Tables[0];
                planList.DataSource = dtmapping;

                DataGridTableStyle dts = new DataGridTableStyle();
                dts.MappingName = dtmapping.TableName;
                DataGridTextBoxColumn col1 = new DataGridTextBoxColumn();
                col1.HeaderText = "计划编号";
                col1.MappingName = "planId";
                col1.Width = 90;
                dts.GridColumnStyles.Add(col1);

                DataGridTextBoxColumn col2 = new DataGridTextBoxColumn();
                col2.HeaderText = "计划名称";
                col2.MappingName = "planName";
                col2.Width = 120;
                dts.GridColumnStyles.Add(col2);

                DataGridTextBoxColumn col3 = new DataGridTextBoxColumn();
                col3.HeaderText = "抄表月份";
                col3.MappingName = "month";
                col3.Width = 60;
                dts.GridColumnStyles.Add(col3);

                DataGridTextBoxColumn col4 = new DataGridTextBoxColumn();
                col4.HeaderText = "数量";
                col4.MappingName = "meterCount";
                col4.Width = 44;
                dts.GridColumnStyles.Add(col4);

                planList.TableStyles.Clear();
                planList.TableStyles.Add(dts);
                planList.Refresh();
            }
            else { MessageBox.Show("没有数据！"); }

        }
        private void planList_Click(object sender, EventArgs e)
        {
            if (planList.CurrentRowIndex == -1)
                return;
            //獲取计划编号
            string PlanID = ((DataTable)planList.DataSource).Rows[planList.CurrentRowIndex][0].ToString();
            Identity.PlanId = PlanID;
            WaterReadingList wrl = new WaterReadingList();
            wrl.Show();
            this.Close();
        }

        private void menuSearch_Click(object sender, EventArgs e)
        {
            WaterRecordSel WRS = new WaterRecordSel();
            WRS.Show();
            this.Close();
           CloseBarCode();
        }

        private void SynchroData_Click(object sender, EventArgs e)
        {
            if (NetWorkState.checkWifiState())
            {
                int tmp = 0;
                DataTable waterData = DataHelper.ReadingWatermeterDao.readedData();
                progressBar1.Visible = true;
                progressBar1.Maximum = waterData.Rows.Count;
                progressBar1.Value = 0;
                for (int i = 0; i < waterData.Rows.Count; i++)
                {
                    string mprid = waterData.Rows[i]["MPRId"].ToString().Trim();
                    string completeDt = waterData.Rows[i]["READINGTIME"].ToString().Trim();
                    string degree = waterData.Rows[i]["READDEGREE"].ToString().Trim();
                    if (DataHelper.ReadingWatermeterDao.updateMeterDegree(mprid, degree, completeDt))
                    {
                        DataHelper.ReadingWatermeterDao.deleteData(mprid);
                        progressBar1.Value++;
                        tmp++;
                    }
                }
                progressBar1.Visible = false;
                MessageBox.Show("成功同步"+tmp+"条记录！");
            }
            else { MessageBox.Show("请连接wifi"); }
            BindData();
        }

        private void menuBack_Click(object sender, EventArgs e)
        {
            FuntionView fun = new FuntionView();
            fun.Show();
            this.Close();
        }


      

        

        }
    }
